
import { registerClass } from "../../../cfw/tools/class_decorator";
import BaseAd from "../../core/base/BaseAd";
import { SDKLogicState, SDKState } from "../../SDKDefine";

@registerClass('KSInsertAd')
export default class KSInsertAd extends BaseAd {
    hide(): void {

    }
    load(): void {
        if (this.ad) {
            this.ad.load();
        }
    }
    destroy(): void {

    }

    // open() {
    //     // console.log('BaseInterstitialAd showAd adUnitID ', this.adUnitID)
    //     this.state = SDKState.loading;
    //     this.destroy()
    //     this.create()
    //     this.show()
    // }


    onClose() {

    }

    show() {
        if (this.ad) {
            let p = this.ad.show()
            p.then(function (result) {
                // 插屏广告展示成功
                console.log(`show interstitial ad success, result is ${result}`)
            }).catch(function (error) {
                console.log('error.code ', error.code)
                // 插屏广告展示失败
                console.log(`show interstitial ad failed, error is ${error}`)
                if (error.code === -10005) {
                    // 表明当前app版本不支持插屏广告，可以提醒用户升级app版本

                }
            })
        }

    }


    create() {
        this.destroy();
        // 创建插屏广告实例，提前初始化
        if (!this.ad) {
            this.ad = this.sdk.createInterstitialAd({
                adUnitId: this.adUnitID
            })
            // this.insertAd.onLoad(this.onLoad.bind(this))
            this.ad.onError(this.onError.bind(this))
            this.ad.onClose(this.onClose.bind(this))
            this.show();
        }
    }

}
